package grg.learn.compose.wanandroid.data.repo

import grg.learn.compose.wanandroid.config.WanAndroidApiService
import grg.learn.compose.wanandroid.data.model.IndexArticle
import grg.learn.compose.wanandroid.data.model.IndexArticlePage
import grg.learn.compose.wanandroid.data.model.IndexBanner
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow

class IndexRepository(
    private val wanAndroidApiService: WanAndroidApiService
) {

    fun getIndexArticles(page: String): Flow<IndexArticlePage?> {
        return flow {
            try {
                emit(wanAndroidApiService.indexArticle(path = page).data)
            } catch (e: Exception) {
                emit(IndexArticlePage.empty())
            }
        }
    }

    fun getIndexBanners(): Flow<List<IndexBanner>>{
        return flow <List<IndexBanner>>{
            try {
                emit(wanAndroidApiService.indexBanner().data ?: emptyList())
            } catch (e: java.lang.Exception) {
                emit(emptyList())
            }
        }
    }

}